package nc.ui.med.bd.establishmentdoc;

import java.util.Collection;

import nc.bs.framework.common.NCLocator;
import nc.bs.framework.exception.ComponentException;
import nc.bs.logging.Logger;
import nc.bs.uap.bd.BDRuntimeException;
import nc.itf.med.bd.establishmentdoc.IEstdoc;
import nc.itf.uap.IUAPQueryBS;
import nc.vo.bd.psndoc.PsnbasdocVO;
import nc.vo.med.bd.establishmentdoc.EstdocVO;
import nc.vo.med.sheshitype.SheshiTypeVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.BusinessRuntimeException;

public class Estdoc_Client {

	private static IEstdoc estdocService;
	private static IUAPQueryBS uapQueryService;

	private static IEstdoc getMbdService() {
		if (estdocService == null) {
			try {
				estdocService = (IEstdoc) NCLocator.getInstance().lookup(IEstdoc.class.getName());
			} catch (ComponentException e) {
				Logger.error(e.getMessage(), e);
				throw new BDRuntimeException("cann't locate IMedbreeddoc");
			}
		}
		return estdocService;
	}
	
	public static IUAPQueryBS getUapQueryService() {
		if (uapQueryService == null) {
			try {
				uapQueryService = (IUAPQueryBS) NCLocator.getInstance().lookup(IUAPQueryBS.class.getName());
			} catch (ComponentException e) {
				Logger.error(e.getMessage(), e);
				throw new BDRuntimeException("cann't locate IMedbreeddoc");
			}
		}
		return uapQueryService;
	}

	public static void delete(EstdocVO arg0) throws Exception {
		getMbdService().deleteEstdocVO(arg0);
	}

	public static java.lang.String insert(EstdocVO arg0) throws Exception {
		return getMbdService().insertEstdocVO(arg0);
	}

	public static void update(EstdocVO arg0) throws Exception {
		getMbdService().updateEstdocVO(arg0);
	}

	public static EstdocVO[] queryAll(java.lang.String arg0) throws Exception {
		return getMbdService().queryEstdocVO(arg0);
	}
	
	public static SheshiTypeVO[] queryCataTypeByCorpPk(String pk_corp){
		
		SheshiTypeVO[] cataTypeVOs = null;
		String where = "pk_corp='" + pk_corp + "' or pk_corp='0001' order by sheshitypecode";
		try {
			Collection c = getUapQueryService().retrieveByClause(SheshiTypeVO.class, where);
			if (c != null && c.size() > 0) {
				cataTypeVOs = (SheshiTypeVO[]) c.toArray(new SheshiTypeVO[c.size()]);
			}
		} catch (BusinessException e) {
			throw new BusinessRuntimeException(e.getMessage());
		}
		return cataTypeVOs;
	}
	
	public static String getPsnbasdocPKcrop(String pk_psnbasdoc){
		if(pk_psnbasdoc==null || pk_psnbasdoc.trim().length()==0){
			return null;
		}
		try {
			PsnbasdocVO psn = (PsnbasdocVO)getUapQueryService().retrieveByPK(PsnbasdocVO.class, pk_psnbasdoc);
			if(psn!=null){
				return psn.getPk_corp();
			}
		} catch (BusinessException e) {
			throw new BusinessRuntimeException(e.getMessage());
		}
		return null;
	}
	
}
